<?php
require_once('config/config.php');
require_once('checked.php'); //判断是否登录

//分页功能
// 初始页面

$page = isset($_GET["page"]) ?trim($_GET["page"])  :1;
$sql = "SELECT COUNT(id) FROM {$pre_}person";
// 总数据
$sum = select($sql);

$sum = (int)$sum['COUNT(id)'];

$sum = isset($sum) ? $sum :0;
// 每次查询的数据
$limit = 3;
// 总页数
$count = ceil($sum/$limit);

$startPag = ($page-1)*$limit;
// 书写sql语句
$sql = "SELECT person.*,depat.name AS depName,job.name AS jobName FROM {$pre_}person AS person LEFT JOIN {$pre_}department AS depat ON depat.id = person.depid LEFT JOIN {$pre_}job AS job ON job.id = person.jobid ORDER BY person.createtime ASC LIMIT {$startPag},{$limit}";
// 接受返回的数组
$list = selectAll($sql);
$pageStr = "<ul>";
if($page<=1){
    $pageStr .="<li><a class='action' Disabled style='color:#000'>首页</a></li>";
    $pageStr .="<li><a class='action' Disabled style='color:#000'>上一页</a></li>";
}else{
    $pagpre = $page - 1;
    $pageStr .="<li><a href='PersonList.php'>首页</a></li>";
    $pageStr .="<li><a href='PersonList.php?page=$pagpre'>上一页</a></li>";
}
for($i= 1;$i<=$count;$i++){
   if($i==$page){
        $pageStr .="<li><a class='action' Disabled style='color:#000'>$i</a></li>";
   }else{
        $pageStr .="<li><a href='PersonList.php?page=$i'>$i</a></li>";
   }
}
if($page>=$count){
    $pageStr .="<li><a class='action' Disabled style='color:#000'>下一页</a></li>";
    $pageStr .="<li><a class='action' Disabled style='color:#000'>尾页</a></li>";
}else{
    $next = $page + 1;
    $pageStr .="<li><a href='PersonList.php?page=$next'>下一页</a></li>";
    $pageStr .="<li><a href='PersonList.php?page=$count'>尾页</a></li>";
}
$pageStr .= "</ul>";


?>


<!DOCTYPE html>
<html lang="en">
    <head>
       <?php require_once('meta.php') ?>
    </head>
    <body>
        <!-- 头部 -->
        <?php require_once('header.php') ?>

        <div class="sidebar-nav">
            <a href="#dashboard-menu" class="nav-header" data-toggle="collapse"><i class="icon-dashboard"></i>控制面板</a>
            <ul id="dashboard-menu" class="nav nav-list collapse in">
                <li><a href="index.php">首页</a></li>
                <li ><a href="list.php">员工管理</a></li>
            </ul>
        </div>

        <div class="content">
            <div class="header">
                <h1 class="page-title">文章列表</h1>
            </div>
            <ul class="breadcrumb">
                <li><a href="index.php">Home</a> <span class="divider">/</span></li>
                <li class="active">Index</li>
            </ul>

            <div class="container-fluid">
                <div class="row-fluid">
                    <div class="btn-toolbar">
                        <button class="btn btn-primary" onClick="location='add.php'"><i class="icon-plus"></i>添加员工</button>
                    </div>
                    <div class="well">
                        <table class="table">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>姓名</th>
                                <th>电话</th>
                                <th>邮箱</th>
                                <th>地址</th>
                                <th>所在部门</th>
                                <th>所在职位</th>
                                <th>入职时间</th>
                                <th>操作</th>
                                <th style="width: 26px;"></th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach($list as $value) {?>
                            <tr>
                                <td><?php echo $value['id']?></td>
                                <td><?php echo $value['nickname']?></td>
                                <td><?php echo $value['mobile']?></td>
                                <td><?php echo $value['email']?></td>
                                <td><?php echo $value['address']?></td>
                                <td><?php echo $value['depName']?></td>
                                <td><?php echo $value['jobName']?></td>
                                <td><?php echo date("Y-m-d",$value['createtime'])?></td>
                                <td>
                                    <a href="add.php"><i class="icon-pencil"></i></a>
                                    <a href="#myModal" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                </td>
                            </tr>
                            <?php }?>
                            </tbody>
                        </table>
                    </div>
                    <div class="pagination">
                        <?php echo $pageStr; ?>
                    </div>

                    <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 id="myModalLabel">Delete Confirmation</h3>
                        </div>
                        <div class="modal-body">
                            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                        </div>
                        <div class="modal-footer">
                            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                            <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                        </div>
                    </div>

                    <footer>
                        <hr>
                        <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                    </footer> 
                </div>
            </div>
        </div>
    </body>
</html>